// const messageUtil = require('../../utils/messageUtil.js')
import messageUtil from "../../utils/messageUtil"
import mobileApi from "../../api/mobileApi"
import commonData from "../../api/data"
import lbkOrderApi from "../../api/lbkOrderApi"
import logApi from "../../api/logApi"

Component({
  mixins: [],
  data: {
    counter: '',
    rootImagePath: "https://web.78keji.cn/v2img/new/keep/",
    formData: {
      username: '',
      phone: '',
      code: '',
      isAgreement: false,
      province: "",
      yys: ""
    },
    mobileList: [
      '177***  3分钟前  领取成功',
      '138***  3分钟前  领取成功',
      '181***  3分钟前  领取成功',
      '139***  3分钟前  领取成功',
      '177***  3分钟前  领取成功',
      '138***  3分钟前  领取成功',
      '181***  3分钟前  领取成功',
      '139***  3分钟前  领取成功',

    ],
    isShowAgreement: false,
    agreementType: 1,
    isShowAgreementConfirm: false,
    isShowSmsConfirm: false,
    timeData: {
      lastTime: 0,
      hour: 23,
      minute: 59,
      second: 59,
      milliseconds: 100
    },
    oder: "",
    showTimeIntervalId: 0,
    mobileList: [

      '177***  抢到了',
      '138***  抢到了',
      '181***  抢到了',
      '139***  抢到了',
      '177***  抢到了',
      '138***  抢到了',
      '181***  抢到了',
      '139***  抢到了',
    ]
  },
  props: {
    rootImagePath: '',
    onGoToPay: data => {
      console.log(data)
    }
  },
  didMount() {
    setTimeout(() => {
      let params = {
        // a: commonData.getA(),
        a:'64357a213e104b02376a788abae01bb5',
        proCode: 'LH2520',
        uuid:this.uuid()
      }
      my.httpRequest({
        url: 'https://www.sx2b2c.com/api/tongji/tongji0520',
        data: params,
        method: "POST",
        headers: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success(rex) {
          console.log(rex)
        },
        fail: function (xhr, type, status) {}
      })
    }, 2000);
    // this.$page.keepAgr
   

  },
  didUpdate() {

  },
  didUnmount() {

  },
  onInit() {



  },
  methods: {

    // initShowTimeMethod() {
    //   let lastTime = new Date();
    //   // lastTime.setHours(23);
    //   // lastTime.setMinutes(59);
    //   // lastTime.setSeconds(59);
    //   lastTime.setMinutes(lastTime.getMinutes() + 5)

    //   let timeData = this.data.timeData
    //   timeData.lastTime = lastTime.getTime();
    //   this.setData({
    //     timeData: timeData
    //   })
    //   let intervalId = setInterval(() => {
    //     this.showTimeMethod();
    //   }, 10);

    //   this.setData({
    //     showTimeIntervalId: intervalId,
    //   })

    // },
    // showTimeMethod() {
    //   var currentDate = new Date(); // 获取当前时间
    //   var time_now = currentDate.getTime();
    //   var time_distance = this.data.timeData.lastTime - time_now; // 结束时间减去当前时间
    //   var int_day, int_hour, int_minute, int_second, int_milliseconds;

    //   let timeData = this.data.timeData

    //   if (time_distance >= 0) {
    //     // 天时分秒换算
    //     int_day = Math.floor(time_distance / 86400000)
    //     time_distance -= int_day * 86400000;
    //     int_hour = Math.floor(time_distance / 3600000)
    //     time_distance -= int_hour * 3600000;
    //     int_minute = Math.floor(time_distance / 60000)
    //     time_distance -= int_minute * 60000;
    //     int_second = Math.floor(time_distance / 1000)
    //     int_milliseconds = currentDate.getMilliseconds()


    //     // 时分秒为单数时、前面加零站位
    //     if (int_hour < 10)
    //       int_hour = "0" + int_hour;
    //     if (int_minute < 10)
    //       int_minute = "0" + int_minute;
    //     if (int_second < 10)
    //       int_second = "0" + int_second;
    //     if (int_milliseconds > 100)
    //       int_milliseconds = parseInt(int_milliseconds / 10);

    //     // 显示时间
    //     // time_d.innerHTML = int_day;

    //     timeData.hour = int_hour;
    //     timeData.minute = int_minute;
    //     timeData.second = int_second;
    //     timeData.milliseconds = int_milliseconds;
    //   } else {
    //     clearInterval(this.data.showTimeIntervalId)
    //     timeData.minute = '00';
    //     timeData.second = '00';
    //     timeData.milliseconds = '00';
    //   }
    //   this.setData({
    //     timeData: timeData
    //   })
    // },
    getAuthPhoneNumber(e) {



    },
    handleAuthError(e) {
      console.log('getPhoneNumber handleAuthError', e)
    },
    agreementConfirmOkHandler() {
      let formData = this.data.formData
      formData.isAgreement = true

      this.setData({
        formData: formData,
        isShowAgreementConfirm: false
      })
      this.submitMethod()
    },
    agreementConfirmCancelHandler() {
      this.setData({
        isShowAgreementConfirm: false
      })
    },
    agreementConfirmShowAgreementHandler(value) {
      let data = {
        target: {
          dataset: {
            attr: value
          }
        }
      }
      this.showAgreementHandler(data)
    },
    bindInput(e) {
      console.log(e)
      if(e.detail.value.length=='11'){
        let formData = this.data.formData
        formData.phone = e.detail.value
      
        this.setData({
          formData: formData
        })
    
        this.submitMethod()
      }
     
    },
    uuid() {
      var s = [];
      var hexDigits = "0123456789abcdef";
      for (var i = 0; i < 36; i++) {
          s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
      }
      s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
      s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
      s[8] = s[13] = s[18] = s[23] = "-";
   
      var uuid = s.join("");
      my.setStorageSync({
        key: 'uuid',
        data: uuid
      })
  
      return uuid;
  },
    submitMethod() {
      my.hideKeyboard()
      var reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      console.log(this.data.formData.phone)
      if (!(reg_tel.test(this.data.formData.phone))) {
        messageUtil.showFail('手机号格式不正确');
        return;
      }


      // formData.value.isAgreement = true;
      if (!this.data.formData.isAgreement) {
        // messageUtil.showFail('请先同意并阅读相关条款')
        console.log('submitMethod2')
        this.setData({
          isShowAgreementConfirm: true
        })
        return;
      }


      console.log('submit', this.data.formData)
      mobileApi.getYys(this.data.formData.phone, getYysRes => {
        my.setStorageSync({
          key: 'mobile',
          data: this.data.formData.phone
        })
        let params = {
          // a:commonData.getA(),
          a:'64357a213e104b02376a788abae01bb5',
          mobile: this.data.formData.phone,
          proCode: 'LH2520',
          proType: 2,
          openid:commonData.getOpenid(),
          // openid:'123',
          uuid:my.getStorageSync({
            key: 'uuid'
          }).data
 
        }
        if (commonData.getParam()) {
          params.param = commonData.getParam()
        }

        // let query = "mobile=" + this.data.formData.phone

        // params.returnUrl = 'http://keep.syhy000.com/h5GotoZfbMini/#/?appid=2021004135681441&url=pages/keeplq/keeplq&query=' + encodeURIComponent(query)
        messageUtil.showLoading()
        lbkOrderApi.submitOrder(params, res => {
          messageUtil.closeLoading()



          // my.ap.openURL({
          //   url:url, // 请将 url 替换为有效的页面地址
          //   success: (res) => {
          //     console.log('openURL success', res)
          //   },
          //   fail: (err) => {
          //     console.log('openURL success', err)
          //   }
          // });

          my.paySignCenter({
            signStr: res.data.str,
            success: (paySignCenterRes) => {
              console.log('zhouqiPayHandler success ' + JSON.stringify(paySignCenterRes))
              console.log(paySignCenterRes.resultStatus);
              if (paySignCenterRes.resultStatus && parseInt(paySignCenterRes.resultStatus) == 6001) {
                messageUtil.closeLoading()
                messageUtil.showFail('支付失败')
                this.props.onGoToPay('支付失败')
                return;
              } else {
                this.props.onGoToPay('成功')
              }

            },
            fail: (paySignCenterResErr) => {
              console.log('zhouqiPayHandler fail ' + JSON.stringify(paySignCenterResErr))
              this.props.onGoToPay('拉起失败')
              logApi.addKeepLog('龙宝卡拉起周期扣款失败：' + JSON.stringify(paySignCenterResErr))
            }
          });

        }, err => {

        })
      })

    },
    inputHandler(e) {
      this.setData({
        formData: {
          phone: e.detail.value
        },
      });
    },
    agreementChange(e) {
      console.log(e)
      let formData = this.data.formData
      formData.isAgreement = !formData.isAgreement
      this.setData({
        formData: formData
      })
    },
    showAgreementHandler(e) {
      this.setData({
        agreementType: parseInt(e.target.dataset.attr),
        isShowAgreement: true
      })
    },
    showAgreementHandlers() {
      let urls='https://web.78keji.cn/xieyi/#/'
      let url =
      'https://ulink.alipay.com/?scheme=alipays%3A%2F%2Fplatformapi%2Fstartapp%3FsaId%3D10000007%26clientVersion%3D3.7.0.0718%26qrcode%3D' +
      encodeURIComponent(encodeURIComponent(urls));
      my.ap.openURL({
            url:url, // 请将 url 替换为有效的页面地址
            success: (res) => {
              console.log('openURL success', res)
            },
            fail: (err) => {
              console.log('openURL success', err)
            }
          });
    },

    agreementOkHandler() {
      this.setData({
        isShowAgreement: false
      })
    },
  },
});